package com.wannengbang.cloudleader.homepage;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ethanhua.skeleton.Skeleton;
import com.ethanhua.skeleton.SkeletonScreen;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.DeviceUnifyListBean;
import com.wannengbang.cloudleader.bean.UserInfoBean;
import com.wannengbang.cloudleader.dialog.CancelGiveAgentDialog;
import com.wannengbang.cloudleader.homepage.adapter.MachinesListAdapter;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.homepage.model.IHomePageModel;
import com.wannengbang.cloudleader.utils.SPManager;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.WrapContentLinearLayoutManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 已划拨 机具列表
 */
public class TransferMachinesListActivity extends BaseActivity implements OnRefreshLoadMoreListener {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_give_agent)
    TextView tvGiveAgent;
    @BindView(R.id.ll_give)
    LinearLayout llGive;

    private IHomePageModel homePageModel;
    private List<DeviceUnifyListBean.DataBean.ItemListBean> beanList;
    private MachinesListAdapter listAdapter;
    private int page = 1;
    private SkeletonScreen skeletonScreen;
    private String type;
    private String bind_agent_id;
    private String status;
    private String agent_id;
    private String agent_name;
    private String is_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tansfer_machines_list);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        refreshLayout.setRefreshHeader(new ClassicsHeader(mActivity));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(mActivity));//设置Footer
        refreshLayout.setOnRefreshLoadMoreListener(this);
        type = getIntent().getStringExtra("type");
        bind_agent_id = getIntent().getStringExtra("bind_agent_id");
        status = getIntent().getStringExtra("status");
        is_send = getIntent().getStringExtra("is_send");

        agent_name = getIntent().getStringExtra("agent_name");
        String titlePrefix = "";
        if (!TextUtils.isEmpty(agent_name)) {
            titlePrefix = agent_name + "_";
            if (agent_name.length() > 6) {
                titlePrefix = agent_name.substring(0, 6) + "_";
            }
        } else {
            titlePrefix = "";
        }
        if ("1".equals(type)) {
            titleBar.setTitle(titlePrefix + "云小宝");
        } else if ("2".equals(type)) {
            titleBar.setTitle(titlePrefix + "云商宝");
        }else if ("3".equals(type)) {
            titleBar.setTitle(titlePrefix + "云小宝Vip版");
        } else if ("4".equals(type)) {
            titleBar.setTitle(titlePrefix + "云小宝Plus版");
        }
    }

    private void initData() {

        String tag = "";
        UserInfoBean userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        if (userInfoBean != null) {
            agent_id = userInfoBean.getData().getId() + "";
        }
//        String[] ids = SPManager.getInstance().getChildAgentId().split("_");
//        List<String> list = Arrays.asList(ids);
//        if (list.contains(bind_agent_id)) {
//            llGive.setVisibility(View.VISIBLE);
//            tag = "1";
//        } else {
//            llGive.setVisibility(View.GONE);
//        }
        homePageModel = new HomePageModelImpl();
        beanList = new ArrayList<>();
        listAdapter = new MachinesListAdapter(beanList, status, tag);
        recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(mActivity));
        recyclerView.setAdapter(listAdapter);

        skeletonScreen = Skeleton.bind(recyclerView)
                .adapter(listAdapter)
                .color(R.color.shimmer_color_1)
                .shimmer(true)
                .angle(20)
                .frozen(false)
                .duration(1000)
                .count(20)
                .load(R.layout.item_skeleton_machines_list)
                .show();

        requestData();
    }


    public void requestData() {
        homePageModel.requestSonIndex(is_send, page, bind_agent_id, type, new DataCallBack<DeviceUnifyListBean>() {
            @Override
            public void onSuccessful(DeviceUnifyListBean bean) {
                if (bean.getData().getItemList() != null && bean.getData().getItemList().size() > 0) {
                    beanList.addAll(bean.getData().getItemList());
                    if (page == 1) {
                        refreshLayout.finishRefresh();
                    } else {
                        refreshLayout.finishLoadMore();
                    }
                } else {
                    if (page != 1) {
                        refreshLayout.finishLoadMoreWithNoMoreData();
                    } else {
                        refreshLayout.finishRefresh();
                    }
                }
                if (beanList.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                } else {
                    llNoData.setVisibility(View.VISIBLE);
                }
                listAdapter.notifyDataSetChanged();
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }

            @Override
            public void onFailed(String msg, String code) {
                if (page == 1) {
                    skeletonScreen.hide();
                }
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        requestData();
        refreshLayout.finishLoadMore(30000);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshData();
    }

    public void refreshData() {
        page = 1;
        beanList.clear();
        requestData();
        refreshLayout.finishRefresh(30000);
    }


    @OnClick({R.id.tv_give_agent})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_give_agent:
                List<String> device_no_list = new ArrayList<>();
                for (DeviceUnifyListBean.DataBean.ItemListBean itemListBean : beanList) {
                    if (itemListBean.isCheck()) {
                        device_no_list.add(itemListBean.getId() + "");
                    }
                }
                CancelGiveAgentDialog dialog = CancelGiveAgentDialog.newInstance(type, device_no_list);
                dialog.show(getSupportFragmentManager(), "dialog");
                dialog.setOnCallBackListener(() -> {
                    refreshData();
                });
                break;
        }
    }
}
